<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Jobs\OrderSubmit;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Carbon\Carbon;
use App\Repository\UpThree\MainOrderRepostition;

class GanPushController extends BaseController
{
  public function __construct()
  {
    $this->log = new Logger('GanPush');
    $this->log->pushHandler(
      (new RotatingFileHandler(storage_path('/logs/GanPush.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
    );
  }
  // 电信那边推送过来的数据 
  public function pushOrderStatus(Request $Request)
  {
    $getData = file_get_contents('php://input');
    $this->log->info('input' . ($getData));
    // return 'SUCCESS';
    // var_dump(1111111111);
    // $getData = 'outer_id=4971879589548764751&plan_mobile_produced=&status=-460&iccid=&sign=f15007f6b2719c4586136d7d73b758b4&tracking_number=&tracking_company=&re
    // ason=2022-08-292022-08-29+00%3A18%3A27%E4%B8%8B%E5%8D%95%E7%94%9F%E4%BA%A7%E8%BF%94%E5%9B%9E%E5%A4%B1%E8%B4%A5%3A%E7%B3%BB%E7%BB%9F%E6%92%A4%E5%8D%95%EF%BC%88%E5%85%A5%E7%BD%91%E8%B5%84%E6%A0%B
    // C%E6%A0%A1%E9%AA%8C%E5%A4%B1%E8%B4%A5%EF%BC%8C%E7%9C%81%E5%86%85%E4%B8%9A%E5%8A%A1%E9%99%90%E5%88%B6%EF%BC%89&is_activated=0&activated_at=&is_recharged=0&recharged_at=';
    // $getData = 'outer_id=HK_20220829102232911348186809&plan_mobile_produced=17752397462&status=550&iccid=&sign=861e354a02a66bc2b4ca315f5624350a&tracking_number=JDV008027944720&tracking_company=%E4%BA%AC%E4%B8%9C%E9%85%8D%E9%80%81&reason=2022-08-29%E6%93%8D%E4%BD%9C%E6%88%90%E5%8A%9F&is_activated=0&activated_at=&is_recharged=0&recharged_at=';
    // 数据处理  
    if (empty($getData)) {
      return 'error';
    }
    $getData =  strToKeyValArrTwo($getData, '=', '&');
    if (empty($getData)) {
      return 'error';
    }
    $statusDatas = [
      0 => '提交中',
      1 => '提交上游',
      // 2 => '拒收单',
      10 => '审核中',
      11 => '待发货',
      20 => '已发货',
      30 => '已签收',
      40 => '已激活',
      50 => '异常单',
      51 => '取消单',
      60 => '失败单'
    ];
    $statusArr = [
      '450' => '待开卡',
      '550' => '已发货',
      '460' => '已领取',
      '500' => '待发货',
      '-460' => '开卡失败',
      '-200' => '身份审核失败',
      '-100' => '待支付',
      '200' => '待审核',
      '120' => '初步审核',
      '10000' => '已激活'
    ];;
    $returnStatusArr = array(
      '550' => 20,
      '-460' => 60,
      '-200' => 50,
      '450' => 1,
      '460' => 30,
      '500' => 11,
      '200' => 10,
      '470' => 10,
      '-100' => 1,
      '120' => 10,
      '0' => 51,
      '10000' => 40

    );
    // 
    // 将数据格式 修改成固定的数据格式
    if (!empty($getData['is_activated']) && $getData['is_activated'] == 1) {
      $getData['status'] = 10000;
    }
    $editData = [
      'status' => $returnStatusArr[$getData['status']],
      'express_company' => !empty($getData['tracking_company']) ? $getData['tracking_company'] : '',
      'express_number' => !empty($getData['tracking_number']) ? $getData['tracking_number'] : '',
      'three_msg' => $getData['reason'],
      'iccid' => !empty($getData['plan_mobile_produced']) ? $getData['plan_mobile_produced'] : '',
      'first_status' => !empty($getData['balanceOne']) ? '1' : '0', // 是否首冲
      'first_status_desc' => !empty($getData['balanceOne']) ? ($getData['balanceOne'] / 100) : '0', // 首冲金额
    ];
    if (!empty($getData['activated_at']) && $getData['activated_at'] != '0000-00-00 00:00:00') {
      $editData['activat_at'] = $getData['activated_at'];
    }
    $where = ['ordersno' => $getData['outer_id']];
    $model = new MainOrderRepostition();
    $res = $model->followOrderStatus($where, $editData);
    $this->log->info('通知参数' . json_encode($getData) . '返回参数' . json_encode($res));
    if (!empty($res['code']) && $res['code'] == 200) {
      return 'SUCCESS';
    }
    return 'error';
  }
}
